{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "d4a763ba",
   "metadata": {},
   "source": [
    "--------\n",
    "* 记录人：庄焕敏\n",
    "* 时间：2021-11-25\n",
    "* week13\n",
    "* 学习线上资源：\n",
    "\n",
    ">[python官方文档](https://python.org)  \n",
    "\n",
    ">[gitee课堂笔记](https://gitee.com/xzhichao/python)\n",
    "\n",
    ">[许老师的博客](https://cnblogs.com/xuzhichao)\n",
    "--------"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b88b1483",
   "metadata": {},
   "source": [
    "# 上周回顾\n",
    "## Json（文本数据）--->Dict(python中的字典数据)\n",
    "## 字典和列表的综合实践"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "51cc19d1",
   "metadata": {},
   "source": [
    "# 本周内容：\n",
    "## [集合]\n",
    "\n",
    ">1.集合的性质\n",
    ">>1.无序（unordered）  \n",
    ">>2.不重复（no duplicate）\n",
    "\n",
    ">2.集合的一般方法  \n",
    ">>1.增加  \n",
    ">>2.删除 \n",
    ">>3.查询：in查询成员关系\n",
    "\n",
    ">3.集合的数学逻辑运算  \n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d77654b8",
   "metadata": {},
   "source": [
    "### 1.集合的性质及概念\n",
    "> * 思考：我想了解一组数据中有哪些唯一存在的值？（假如是列表）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "1b5c922a",
   "metadata": {},
   "outputs": [],
   "source": [
    "no_duplicate_age=[]\n",
    "list_age = [18,24,25,24,18,23,18]\n",
    "for i in list_age:\n",
    "    if i not in no_duplicate_age:\n",
    "        no_duplicate_age.append(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "0d426f05",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[18, 24, 25, 23]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "no_duplicate_age   #唯一"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "894ea5f1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{24, 25, 18, 23}\n"
     ]
    }
   ],
   "source": [
    " # list --->set的方法：set（）\n",
    "no_duplicate_age_set = set(list_age)\n",
    "print(no_duplicate_age_set)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "44ba230b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'orange', 'apple', 'banana', 'pear'}\n",
      "True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "# 官方文档测试代码\n",
    "# 1. 移除重复项\n",
    "basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}\n",
    "print(basket)\n",
    "# 2. in 查询成员关系\n",
    "print('orange' in basket)\n",
    "print('crabgrass' in basket)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "11b7840e",
   "metadata": {},
   "source": [
    "### 集合的一般方法\n",
    "* 集合的创建的两种一般方法：  \n",
    "\n",
    "> 1.set(list)   \n",
    "> 2.直接按照集合的结构创建：{value1,value2,value3,...}   \n",
    "\n",
    ">1.增加add()   \n",
    ">>1.结果:Add an element to a set  \n",
    ">>2.注意:This has no effect if the element is already present  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "29bba0c4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{0, 1, 66, 3, 4, 7, 8}\n"
     ]
    }
   ],
   "source": [
    "set_1 = set([1,3,4,0,8,66,7])\n",
    "print(set_1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "5439176a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function add:\n",
      "\n",
      "add(...) method of builtins.set instance\n",
      "    Add an element to a set.\n",
      "    \n",
      "    This has no effect if the element is already present.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(set_1.add)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "e051a671",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{0, 1, 66, 3, 4, 99, 7, 8}\n"
     ]
    }
   ],
   "source": [
    "set_1.add(99)\n",
    "print(set_1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "41ab520e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{0, 1, 66, 3, 4, 99, 7, 8}\n"
     ]
    }
   ],
   "source": [
    "set_1.add(66)\n",
    "print(set_1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "779dfdfe",
   "metadata": {},
   "source": [
    "> 2.删除\n",
    ">>1.pop():Remove and return an arbitrary set element.Raises KeyError if the set is    empty.    \n",
    ">>2.remove()  \n",
    ">>3.discard()  \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "f1941f9e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{0, 1, 66, 3, 4, 7, 8}\n"
     ]
    }
   ],
   "source": [
    "set_1=set([1,3,4,0,8,66,7])\n",
    "print(set_1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "fef7ceb8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function pop:\n",
      "\n",
      "pop(...) method of builtins.set instance\n",
      "    Remove and return an arbitrary set element.\n",
      "    Raises KeyError if the set is empty.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(set_1.pop)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "26b8e74d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{0, 1, 66, 3, 4, 7, 8}\n",
      "{1, 66, 3, 4, 7, 8}\n"
     ]
    }
   ],
   "source": [
    "# 1. Remove and return an arbitrary set element.\n",
    "print(set_1)\n",
    "set_1.pop()\n",
    "print(set_1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "0dc7b203",
   "metadata": {},
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "'pop from an empty set'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "\u001b[1;32mC:\\Users\\ZHUANG~1\\AppData\\Local\\Temp/ipykernel_30328/3079797475.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;31m# 2. Raises KeyError if the set is empty.\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[0mset_2\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mset\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[0mset_2\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpop\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m: 'pop from an empty set'"
     ]
    }
   ],
   "source": [
    "# 2. Raises KeyError if the set is empty.\n",
    "set_2 = set([])\n",
    "set_2.pop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "15860907",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function remove:\n",
      "\n",
      "remove(...) method of builtins.set instance\n",
      "    Remove an element from a set; it must be a member.\n",
      "    \n",
      "    If the element is not a member, raise a KeyError.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(set_1.remove)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "3114a102",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1, 66, 3, 4, 7, 8}\n",
      "{1, 3, 4, 7, 8}\n"
     ]
    }
   ],
   "source": [
    "# 1. Remove an element from a set; it must be a member.\n",
    "print(set_1)\n",
    "set_1.remove(66)\n",
    "print(set_1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "37e98ea2",
   "metadata": {},
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "66",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "\u001b[1;32mC:\\Users\\ZHUANG~1\\AppData\\Local\\Temp/ipykernel_14348/3603598176.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;31m# 2. If the element is not a member, raise a KeyError. 一般项目中 remove（）会和 in 共同使用\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mset_1\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mremove\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m66\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m: 66"
     ]
    }
   ],
   "source": [
    "# 2. If the element is not a member, raise a KeyError. 一般项目中 remove（）会和 in 共同使用\n",
    "set_1.remove(66)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "36982fc6",
   "metadata": {},
   "outputs": [],
   "source": [
    "# * 避免KeyError的方式：\n",
    "if 66 in set_1:\n",
    "    set_1.remove(66)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "0a98405e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function discard:\n",
      "\n",
      "discard(...) method of builtins.set instance\n",
      "    Remove an element from a set if it is a member.\n",
      "    \n",
      "    If the element is not a member, do nothing.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(set_1.discard)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "c616315e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1, 3, 4, 7, 8}\n"
     ]
    }
   ],
   "source": [
    "print(set_1)\n",
    "set_1.discard(66)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "c17546dd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function remove:\n",
      "\n",
      "remove(...) method of builtins.set instance\n",
      "    Remove an element from a set; it must be a member.\n",
      "    \n",
      "    If the element is not a member, raise a KeyError.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(set_1.remove)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3aedc4e1",
   "metadata": {},
   "source": [
    "### 3.集合的运算\n",
    "> 1.集合的交集  \n",
    "> 2.集合的并集  \n",
    "> 3.集合的差集  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "13df377a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "set_1 :  {1, 2, 3, 4, 5, 6, 7}\n",
      "set_2 :  {0, 1, 66, 3, 4, 7, 8}\n"
     ]
    }
   ],
   "source": [
    "set_1 = set([1,2,5,6,7,2,3,2,4,4])\n",
    "set_2 = set([1,3,4,0,8,66,7])\n",
    "print(\"set_1 : \",set_1)\n",
    "print(\"set_2 : \",set_2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c1fe2296",
   "metadata": {},
   "source": [
    "> 1.集合的交集\n",
    ">> 1.使用集合的方法：set_1.intersection(set_2)     \n",
    ">> 2.使用逻辑运算符：set_1 & set_2 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "b5e24803",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function intersection:\n",
      "\n",
      "intersection(...) method of builtins.set instance\n",
      "    Return the intersection of two sets as a new set.\n",
      "    \n",
      "    (i.e. all elements that are in both sets.)\n",
      "\n"
     ]
    }
   ],
   "source": [
    " help(set_1.intersection)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "34464d91",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "set_1: {1, 2, 3, 4, 5, 6, 7}\n",
      "set_2: {0, 1, 66, 3, 4, 7, 8}\n",
      "交集结果： {1, 3, 4, 7}\n"
     ]
    }
   ],
   "source": [
    "# 使用集合的方法：set_1.intersection(set_2)\n",
    "print(\"set_1:\",set_1)\n",
    "print(\"set_2:\",set_2)\n",
    "print(\"交集结果：\",set_1.intersection(set_2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "b0e3edd6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "set_1 :  {1, 2, 3, 4, 5, 6, 7}\n",
      "set_2 :  {0, 1, 66, 3, 4, 7, 8}\n",
      "交集结果 :  {1, 3, 4, 7}\n"
     ]
    }
   ],
   "source": [
    "# 使用逻辑运算符：set_1 & set_2\n",
    "print(\"set_1 : \",set_1)\n",
    "print(\"set_2 : \",set_2)\n",
    "print(\"交集结果 : \",set_1 & set_2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "159ecdb0",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 实践：\n",
    "set_1_xu = {\"星期一12-14\",\"星期二3-5\",\"星期二12-14\"}\n",
    "set_2_前端 = {\"星期二3-5\",\"星期二12-14\"}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "910342d0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'星期二3-5', '星期二12-14'}\n",
      "结论：周一班 API课 让给前端的同学~\n"
     ]
    }
   ],
   "source": [
    "print(set_1_xu & set_2_前端)\n",
    "print(\"结论：周一班 API课 让给前端的同学~\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f79b77ea",
   "metadata": {},
   "source": [
    "> 2.集合的并集\n",
    ">> 1. 使用并集的方法:set_1.union(set_2)\n",
    ">> 2. 使用逻辑运算符：set_1 | set_2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "c20d40a6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "set_1 :  {1, 2, 3, 4, 5, 6, 7}\n",
      "set_2 :  {0, 1, 66, 3, 4, 7, 8}\n",
      "交集结果 :  {0, 1, 2, 3, 4, 5, 6, 7, 66, 8}\n"
     ]
    }
   ],
   "source": [
    "# 1.使用并集的方法：set_1.union(set_2)\n",
    "print(\"set_1 : \",set_1)\n",
    "print(\"set_2 : \",set_2)\n",
    "print(\"交集结果 : \",set_1.union(set_2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "6f973823",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "set_1 :  {1, 2, 3, 4, 5, 6, 7}\n",
      "set_2 :  {0, 1, 66, 3, 4, 7, 8}\n",
      "交集结果 :  {0, 1, 2, 3, 4, 5, 6, 7, 66, 8}\n"
     ]
    }
   ],
   "source": [
    "# 2. 使用逻辑运算符：set_1 | set_2\n",
    "print(\"set_1 : \",set_1)\n",
    "print(\"set_2 : \",set_2)\n",
    "print(\"交集结果 : \",set_1 | set_2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "3909cc6c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "开始的时间 :  1638891380.6750407\n",
      "set_1 :  {1, 2, 3, 4, 5, 6, 7}\n",
      "set_2 :  {0, 1, 66, 3, 4, 7, 8}\n",
      "交集结果 :  {0, 1, 2, 3, 4, 5, 6, 7, 66, 8}\n",
      "结束的时间 :  1638891380.6750407\n",
      "运行的时间 :  0.0\n"
     ]
    }
   ],
   "source": [
    "# 计算运行效率 = 运行后的时间节点 - 运行前的时间节点  : 思考并对比列表运算的执行效率 \n",
    "import time\n",
    "start_time = time.time()\n",
    "print(\"开始的时间 : \",start_time)\n",
    "print(\"set_1 : \",set_1)\n",
    "print(\"set_2 : \",set_2)\n",
    "print(\"交集结果 : \",set_1 | set_2)\n",
    "stop_time = time.time()\n",
    "print(\"结束的时间 : \",stop_time)\n",
    "print(\"运行的时间 : \", stop_time - start_time)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f6b9e769",
   "metadata": {},
   "source": [
    "> 3.集合的差集（考虑先后顺序）\n",
    ">> 1.使用差集的方法：set_1.difference(set_2)          \n",
    ">> 2.使用逻辑运算符：set_1 - set_2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "9b43129b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "set_1 :  {1, 66, 3, 4, 7, 8}\n",
      "set_2 :  set()\n",
      "set_1 和 set_2 差集结果 :  {1, 66, 3, 4, 7, 8}\n"
     ]
    }
   ],
   "source": [
    "print(\"set_1 : \",set_1)\n",
    "print(\"set_2 : \",set_2)\n",
    "print(\"set_1 和 set_2 差集结果 : \",set_1.difference(set_2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "032cf414",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "set_1 :  {1, 66, 3, 4, 7, 8}\n",
      "set_2 :  set()\n",
      "set_2 和 set_1 差集结果 :  set()\n"
     ]
    }
   ],
   "source": [
    "print(\"set_1 : \",set_1)\n",
    "print(\"set_2 : \",set_2)\n",
    "print(\"set_2 和 set_1 差集结果 : \",set_2.difference(set_1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "9024cd98",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "set_1 :  {1, 66, 3, 4, 7, 8}\n",
      "set_2 :  set()\n",
      "set_1 和 set_2 差集结果 :  {1, 66, 3, 4, 7, 8}\n"
     ]
    }
   ],
   "source": [
    "print(\"set_1 : \",set_1)\n",
    "print(\"set_2 : \",set_2)\n",
    "print(\"set_1 和 set_2 差集结果 : \",set_1 - set_2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "2456bc1e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "set_1 :  {1, 66, 3, 4, 7, 8}\n",
      "set_2 :  set()\n",
      "set_2 和 set_1 差集结果 :  set()\n"
     ]
    }
   ],
   "source": [
    "print(\"set_1 : \",set_1)\n",
    "print(\"set_2 : \",set_2)\n",
    "print(\"set_2 和 set_1 差集结果 : \",set_2 - set_1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3018cad6",
   "metadata": {},
   "source": [
    "> 4.对称差集\n",
    ">> 1.用对称差集的方法：set_1.symmetric_difference(set_2)   \n",
    ">> 2.使用逻辑运算符：? (set_1 - set_2) | (set_2-set_1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "e9de826d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function symmetric_difference:\n",
      "\n",
      "symmetric_difference(...) method of builtins.set instance\n",
      "    Return the symmetric difference of two sets as a new set.\n",
      "    \n",
      "    (i.e. all elements that are in exactly one of the sets.)\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(set_1.symmetric_difference)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "69c48a69",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "set_1 :  {1, 66, 3, 4, 7, 8}\n",
      "set_2 :  set()\n",
      "对称差集结果 :  {1, 66, 3, 4, 7, 8}\n"
     ]
    }
   ],
   "source": [
    "print(\"set_1 : \",set_1)\n",
    "print(\"set_2 : \",set_2)\n",
    "print(\"对称差集结果 : \",set_1.symmetric_difference(set_2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "6653492c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "set_1 :  {1, 66, 3, 4, 7, 8}\n",
      "set_2 :  set()\n",
      "对称差集结果 :  {1, 66, 3, 4, 7, 8}\n"
     ]
    }
   ],
   "source": [
    "print(\"set_1 : \",set_1)\n",
    "print(\"set_2 : \",set_2)\n",
    "print(\"对称差集结果 : \",(set_1 - set_2) | (set_2-set_1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "39ea0278",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{0, 1, 3, 4, 99, 7, 8}\n"
     ]
    }
   ],
   "source": [
    "set_1.discard(66)\n",
    "print(set_1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "99c5f4bb",
   "metadata": {},
   "source": [
    "###  集合实践：（129）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "278d9db4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "vowels :  {'u', 'o', 'e', 'a', 'i'}\n",
      "Provide a word to search for vowels:hello huanmin\n",
      "word :  {'l', 'u', 'o', 'm', ' ', 'e', 'a', 'h', 'i', 'n'}\n",
      "{'u', 'o', 'e', 'a', 'i'}\n"
     ]
    }
   ],
   "source": [
    "vowels = set('aeiou')\n",
    "print(\"vowels : \",vowels)\n",
    "word = input(\"Provide a word to search for vowels:\")\n",
    "print(\"word : \",set(word))\n",
    "found = vowels.intersection(set(word))\n",
    "print(found)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f59bb8c5",
   "metadata": {},
   "source": [
    "#  课后作业："
   ]
  },
  {
   "cell_type": "markdown",
   "id": "78d517e0",
   "metadata": {},
   "source": [
    "> 课堂练习     \n",
    "> 课本第三章 课后项目练习          \n",
    "> 总结集合方法     "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "238d2b15",
   "metadata": {},
   "source": [
    "## 课本第三章 课后项目练习"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "68e89c93",
   "metadata": {},
   "outputs": [],
   "source": [
    "person3 = {'name':'Peng Yuan',\n",
    "           'Gender':'Male',\n",
    "           'Occupation':'Researcher',\n",
    "           'Home Planet':'Betelgeuse Seven' }"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "096512a3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'name': 'Peng Yuan',\n",
       " 'Gender': 'Male',\n",
       " 'Occupation': 'Researcher',\n",
       " 'Home Planet': 'Betelgeuse Seven'}"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "person3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "2ac18290",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Peng Yuan'"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 中括号可以用来索引\n",
    "# 列表使用数值索引值来访问数据，字典使用键来访问其关联的数据值\n",
    "person3['name']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "383dc296",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 增加\n",
    "person3['Age'] = 38"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "80f94318",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'name': 'Peng Yuan',\n",
       " 'Gender': 'Male',\n",
       " 'Occupation': 'Researcher',\n",
       " 'Home Planet': 'Betelgeuse Seven',\n",
       " 'Age': 38}"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "person3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "0de18109",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{}"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "found = {}\n",
    "found"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "779dba0f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a': 0, 'e': 0, 'i': 0, 'o': 0, 'u': 0}"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "found['a'] = 0\n",
    "found['e'] = 0\n",
    "found['i'] = 0\n",
    "found['o'] = 0\n",
    "found['u'] = 0\n",
    "found"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "36ec3321",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a': 0, 'e': 2, 'i': 0, 'o': 0, 'u': 0}"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "found\n",
    "found['e'] = found['e']+1 # 将“found['e']”指示的值增1\n",
    "found"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "2a53aa0f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a': 0, 'e': 3, 'i': 0, 'o': 0, 'u': 0}"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "found['e'] += 1\n",
    "found"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "9ed2273b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a was found 0 time(s.)\n",
      "e was found 3 time(s.)\n",
      "i was found 0 time(s.)\n",
      "o was found 0 time(s.)\n",
      "u was found 0 time(s.)\n"
     ]
    }
   ],
   "source": [
    "for k in found:\n",
    "    print(k,'was found',found[k],'time(s.)')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "fe7f9994",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a was found 0 time(s).\n",
      "e was found 3 time(s).\n",
      "i was found 0 time(s).\n",
      "o was found 0 time(s).\n",
      "u was found 0 time(s).\n"
     ]
    }
   ],
   "source": [
    "for k in sorted(found):\n",
    "    print(k,'was found',found[k],'time(s).')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "c4c032e7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a was found 0 time(s).\n",
      "e was found 3 time(s).\n",
      "i was found 0 time(s).\n",
      "o was found 0 time(s).\n",
      "u was found 0 time(s).\n"
     ]
    }
   ],
   "source": [
    "for k,v in sorted(found.items()):\n",
    "    print(k,'was found',v,'time(s).')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "7952dae2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Provide a word to search for vowels:huamin\n",
      "a was found 0 time(s).\n",
      "e was found 0 time(s).\n",
      "i was found 0 time(s).\n",
      "o was found 0 time(s).\n",
      "u was found 1 time(s).\n",
      "a was found 1 time(s).\n",
      "e was found 0 time(s).\n",
      "i was found 0 time(s).\n",
      "o was found 0 time(s).\n",
      "u was found 1 time(s).\n",
      "a was found 1 time(s).\n",
      "e was found 0 time(s).\n",
      "i was found 1 time(s).\n",
      "o was found 0 time(s).\n",
      "u was found 1 time(s).\n"
     ]
    }
   ],
   "source": [
    "# items()方法\n",
    "vowels = ['a','e','i','o','u']\n",
    "word = input('Provide a word to search for vowels:')\n",
    "\n",
    "found = {}\n",
    "\n",
    "found['a'] = 0\n",
    "found['e'] = 0\n",
    "found['i'] = 0\n",
    "found['o'] = 0\n",
    "found['u'] = 0\n",
    "\n",
    "for letter in word:\n",
    "    if letter in vowels:\n",
    "        found[letter] += 1\n",
    "        for k,v in sorted(found.items()):\n",
    "            print(k,'was found',v,'time(s).')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "aa176b37",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Provide a word to search for vowels:huan\n",
      "u was found 1 time(s).\n",
      "a was found 1 time(s).\n",
      "u was found 1 time(s).\n"
     ]
    }
   ],
   "source": [
    "# # setdefault 方法\n",
    "vowels = ['a','e','i','o','u']\n",
    "word = input('Provide a word to search for vowels:')\n",
    "\n",
    "found = {}\n",
    "\n",
    "for letter in word:\n",
    "    if letter in vowels:\n",
    "        found.setdefault(letter,0)\n",
    "        found[letter] += 1\n",
    "        for k,v in sorted(found.items()):\n",
    "            print(k,'was found',v,'time(s).')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a2f099c7",
   "metadata": {},
   "source": [
    "## 总结集合方法"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "196c8845",
   "metadata": {},
   "source": [
    "> 1. 集合的性质：无序、不重复    \n",
    "> 2. 集合的一般方法：  \n",
    ">> 集合创建的两种一般方法：   \n",
    ">> set(list)    \n",
    ">> 直接按照集合的结构创建：{value1,value2,value3,...}   \n",
    "\n",
    ">> 1. 增加\n",
    ">>> 1. add()\n",
    ">> 2. 删除\n",
    ">>> 1. pop()\n",
    ">>> 2. remove()\n",
    ">>> 3. discard()\n",
    ">> 3. 查询\n",
    ">>> 1. in查询成员关系"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e12c681e",
   "metadata": {},
   "source": [
    "\n",
    "\n",
    "|集合的运算    | 使用集合的方法       | 使用逻辑运算符  |\n",
    "|:-----------:| -------------:|-------------:|\n",
    "|集合的交集  | set_1.intersection(set_2) | set_1 & set_2|\n",
    "|集合的并集 |  set_1.union(set_2) |set_1 | set_2|\n",
    "|集合的差集   | set_1.difference(set_2) | set_1 - set_2|\n",
    "|对称差集     | set_1.symmetric_difference(set_2) |  ?(set_1 - set_2) | (set_2 - set_1)|\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "edc83d55",
   "metadata": {},
   "outputs": [],
   "source": [
    "|"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.0b3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
